<%
    Users user = Trader.getUser(request.getParameter("uid"));
        wstrader.task.traffic.physical.Contract task = user.traffic.physical.contract;

        if (!user.Authenticated()) {
                response.sendRedirect("logon.jsp");
                return;
        }
        ((Tasks)task.getOwner()).clearFocus();
        task.setFocus(true);	
        Trader.window_width = "90%";

%>

<%@include file="../header.jsp"%>

<%
	
        ResultSet rs=null;
        long focused_id = task.doTask();	
        Record ref = task.getRecord("contracts",focused_id);
        Record rcDI = new Record(task.getDeliveryInstruction_record());
        Record rcSI = new Record(task.getShippingInstruction_record());
%>
<link href="style.css" rel="stylesheet" type="text/css">

<script language="javascript">
    function doNewDi()
    {
        var form = document.formMain;
        if (form.contracts.value > 0)
        {
            if (form.contract_type.value == "Purchase Contract")
            {
                form.new_rec.value="1";
                form.action = "traffic.di.card-view.jsp"
                form.submit();
            }
            else
            {
                alert("You cannot create a DI for sales contract");
            }
		
        }
        else
        {
            alert ("Please select a contract");
        }
    }

    function doNewSi()
    {
        var form = document.formMain;
        if (form.contracts.value > 0)
        {
            if (form.contract_type.value == "Sales Contract")
            {
                form.new_rec.value="1";
                form.action = "traffic.si.card-view.jsp"
                form.submit();
            }
            else
            {
                alert("You cannot create a SI for purchase contract");
            }
        }
        else
        {
            alert ("Please select a contract");
        }
    }

    function cardViewClick_di(row)
    {
        var form = document.formMain;
        form.delivery_instruction.value = row.id;
        form.action = "traffic.di.card-view.jsp";
        form.submit();
        
        /*$.ajax({
            type: "GET",
            url: "DI_Controller",
            data:"action=getDIMillInfo&trader_id=" + row.id,
            success: function(msg){
                if (msg != "undefined"){
                    alert(msg);   
                    var form = document.formMain;
                    form.delivery_instruction.value = row.id;
                    form.action = "traffic.di.card-view.jsp";
                    form.submit();
                }else{
                    alert("mission failed");
                        
                }
            }
        });*/
        
    }

    function cardViewClick_si(row)
    {
        var form = document.formMain;
        form.shipping_instruction.value = row.id;
        form.action = "traffic.si.card-view.jsp";
        form.submit();
    }

    function doListView()
    {
        var form = document.formMain;
        form.action = "traffic.physical.jsp";
        form.submit();
    }
</script>
<form method="POST" name="formMain" action="" onSubmit="">				  
    <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
        <tr>
            <td colspan="2"><img src="images/contracts.jpg" width="250" height="30"></td>
        </tr>
        <tr bgcolor="#EEEEEE" class="style3" align="center" style="font-weight:bold">
            <td width="14%">Contracts</td>
            <td width="86%" align="left"><label id="e16">Details</label></td>
        </tr>
        <tr>
            <td valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0" class="style2">
                    <tr>
                        <td><select name="contracts" size=19 class="style11" id="contracts" style="width:100%;" onChange="doPost();">
                                <%@include file="include/ContractRefNoList.jsp"%>
                            </select></td>
                    </tr>
                    <tr>
                        <td>&nbsp;
                        </td>
                    </tr>
                </table>
            </td>
            <td valign="top"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
                    <tr>
                        <td><table width="100%"  border="1" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td height="270"><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                            <tr>
                                                <td width="45%"><table width="100%"  border="0" cellpadding="1" cellspacing="0" class="style2">
                                                        <tr>
                                                            <td width="35%" align="right">Contract Ref &nbsp;</td>
                                                            <td width="65%"><strong><%=ref.getString("ref_number")%></strong></td>
                                                        </tr>
                                                        <%
                                                                String type ="";
                                                                if (ref.getString("type").compareTo("P")==0) type ="Purchase Contract";
                                                                if (ref.getString("type").compareTo("S")==0) type ="Sales Contract";	
                                                        %>						  
                                                        <tr>
                                                            <td align="right">Contract Type &nbsp;</td>
                                                            <td><input type="text" name="contract_type" id="contract_type" class="style2" style="width:50%;" value="<%=type%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Contract No </td>
                                                            <td><p>
                                                                    <input type="text" name="contract_no" id="contract_no" class="style2" style="width:50%;" value="<%=ref.getString("contract_no")%>" readonly="true">
                                                                </p>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <td align="right">Supplier/Buyer &nbsp;</td>
                                                            <td><input type="text" name="supplier_buyer_id" id="supplier_buyer_id" class="style2" style="width:50%;" value="<%=task.getFieldValueForId("company_master","name",ref.getIntValue("supplier_buyer_id"))%>" readonly="true"></td>
                                                        </tr>

                                                        <tr>
                                                            <td align="right">Supplier/Buyer Ref &nbsp;</td>
                                                            <td><input type="text" name="supplier_buyer_ref" id="supplier_buyer_ref" class="style2" style="width:50%;" value="<%=ref.getString("supplier_buyer_ref")%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Packing &nbsp;</td>
                                                            <td><input type="text" name="packing_id" id="packing_id" class="style2" style="width:50%;" value="<%=task.getFieldValueForId("packing_master","name",ref.getIntValue("packing_id"))%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Quantity &nbsp;</td>
                                                            <td><input type="text" name="tons" id="tons" class="style2" style="width:80px; text-align:right" value="<%=ref.getFloatValue("tons")%>" onChange="tonsChanged();" readonly="true">&nbsp;Mts </td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Kg per Bag &nbsp;</td>
                                                            <td><input type="text" name="kg_per_bag" id="kg_per_bag" class="style2" style="width:80px; text-align:right" value="<%=ref.getFloatValue("kg_per_bag")%>" onChange="tonsChanged()" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">No of Bags &nbsp;</td>
                                                            <td><input type="text" name="no_of_bags" id="no_of_bags" class="style2" style="width:80px; text-align:right" value="<%=ref.getInt("no_of_bags")%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">&nbsp;</td>
                                                            <td>&nbsp;</td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Completed &nbsp;</td>
                                                            <td><input type="checkbox" name="completed_" id="completed_" value="2" onClick="cbxClick(this,2,1)" <%=ref.getIntValue("status")==2?"checked":""%>></td>
                                                        <input type="hidden" name="completed"  id="completed"  value="<%=ref.getIntValue("status")%>">
                                                        </tr>
                                                    </table></td>
                                                <td valign="top"><table width="100%"  border="0" cellpadding="1" cellspacing="0" class="style2">
                                                        <tr>
                                                            <td width="30%" align="right">Origin &nbsp;</td>
                                                            <td><input type="text" name="origin_id" id="origin_id" class="style2" style="width:50%;" value="<%=task.getFieldValueForId("origin_master","name",ref.getIntValue("origin_id"))%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Quality &nbsp;</td>
                                                            <td><input type="text" name="quality_id" id="quality_id" class="style2" style="width:50%;" value="<%=task.getFieldValueForId("quality_master","name",ref.getIntValue("quality_id"))%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Grade &nbsp;</td>
                                                            <td><input type="text" name="grade_id" id="grade_id" class="style2" style="width:50%;" value="<%=task.getFieldValueForId("grade_master","name",ref.getIntValue("grade_id"))%>" readonly="true"></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">&nbsp;</td>
                                                            <td>&nbsp;</td>
                                                        </tr>

                                                        <tr>
                                                            <td align="right">Delivery Period &nbsp;</td>
                                                            <td><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
                                                                    <tr>
                                                                        <td width="27%">From</td>
                                                                        <td width="26%">&nbsp;</td>
                                                                        <td width="27%">To</td>
                                                                        <td width="20%">&nbsp;</td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td><input type="text" name="first_date" id="first_date" class="style2" style="width:70px; text-align:center" value="<%=ref.getDate("first_date")%>" readonly="true"></td>
                                                                        <td><a  onclick="popUpCalendar(this,'first_date','dd-mmm-yy');"><IMG width="19" height="19" border="0" src="js/calendar.gif" title="dd-MMM-yy (example: 01-Jan-04)"></a></td>
                                                                        <td><input type="text" name="last_date" id="last_date" class="style2" style="width:70px; text-align:center" value="<%=ref.getDate("last_date")%>" readonly="true"></td>
                                                                        <td><a onclick="popUpCalendar(this,'last_date','dd-mmm-yy');"><IMG width="20" height="20" border="0" src="js/calendar.gif" title="dd-MMM-yy (example: 01-Jan-04)"></a></td>
                                                                    </tr>
                                                                </table></td>
                                                        </tr>
                                                        <tr>
                                                            <td align="right">Remarks &nbsp;</td>
                                                            <td><textarea name="remarks" id="remarks" rows="4" class="style2" style="width:100%" readonly><%=ref.getString("remarks")%></textarea></td>
                                                        </tr>
                                                    </table></td>
                                            </tr>
                                        </table></td>
                                </tr>
                            </table></td>
                    </tr>
                    <tr>
                        <td>
                            <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td height="24">&nbsp;</td>
                                    <td align="right"><img src="images/listview.jpg" width="55" height="18" onClick="doListView();"></td>
                                </tr>
                            </table></td>
                    </tr>
                    <tr>
                        <td><table width="100%"  border="1" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">				  
                                            <tr>
                                                <td align="center" height="24"><strong>Delivery Instructions 
                                                        <input name="delivery_instruction" type="hidden" id="delivery_instruction" value="0">
                                                    </strong></td>
                                            </tr>
                                            <tr>
                                                <td><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                                        <tr style="font-weight:bold" bgcolor="#EEEEEE" align="center">
                                                            <td width="31" rowspan="2">No</td>
                                                            <td width="120" rowspan="2">DI Ref </td>
                                                            <td width="150" rowspan="2">Destination</td>
                                                            <td colspan="2">Quantity</td>
                                                            <td colspan="2">Delivered</td>
                                                            <td colspan="3">Delivery Period </td>
                                                        </tr>
                                                        <tr style="font-weight:bold" bgcolor="#EEEEEE" align="center">
                                                            <td width="70">Tons</td>
                                                            <td width="70">Bags</td>
                                                            <td width="70">Tons</td>
                                                            <td width="70">Bags</td>
                                                            <td width="68">Date</td>
                                                            <td width="68">From</td>
                                                            <td>To</td>
                                                        </tr>
                                                    </table></td>
                                            </tr>
                                            <tr>
                                                <td><div style="overflow:scroll; height:150px;">
                                                        <table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                                            <%
                                                                    rcDI.beforeFirst();
                                                                    while (rcDI.next())
                                                                    {
                                                            %>				  
                                                            <tr id = <%=rcDI.getIntValue("id")%> class="style11" onClick="highlightOn(this);" onDblClick="cardViewClick_di(this);">
                                                                <td width="31" align="center" bgcolor="#EEEEEE"><%=rcDI.getIntValue("id")%></td>
                                                                <td width="120"><%=rcDI.getString("ref_number")%></td>
                                                                <td width="150"><%=task.getFieldValueForId("warehouse_master","name",rcDI.getIntValue("warehouse_id"))%></td>
                                                                <td width="70" align="right"><%=rcDI.getFloatValue("tons")%></td>
                                                                <td width="70" align="right"><%=rcDI.getFloatValue("no_of_bags")%></td>
                                                                <td width="70" align="right"><%=Utils.numberToStr((rcDI.getFloatValue("recdton")/1000),3)%></td>
                                                                <td width="70" align="right"><%=rcDI.getFloatValue("recdbag")%></td>
                                                                <td width="68" align="center"><%=rcDI.getDate("delivery_date")%></td>
                                                                <td width="68" align="center"><%=rcDI.getTime("from_time")%></td>
                                                                <td align="center"><%=rcDI.getTime("to_time")%></td>
                                                            </tr>
                                                            <%
                                                                    }
                                                            %>				  
                                                        </table>
                                                    </div></td>
                                            </tr>
                                            <tr><td><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                                        <tr class="style2" bgcolor="#EEEEEE">
                                                            <td width="31" align="center"><img src="images/new.gif" width="15" height="15" title="Create New DI" onClick="doNewDi();"></td>
                                                            <td width="274" align="center"><strong>Total</strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td align="center">&nbsp;</td>
                                                            <td width="15" align="center">&nbsp;</td>
                                                        </tr>
                                                    </table></td></tr>					  
                                        </table></td>
                                </tr>
                                <tr>
                                    <td><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="style2">
                                            <tr>
                                                <td height="24" align="center"><strong>Shipping Instructions 
                                                        <input name="shipping_instruction" type="hidden" id="shipping_instruction" value="0">
                                                    </strong></td>
                                            </tr>
                                            <tr>
                                                <td><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                                        <tr style="font-weight:bold" bgcolor="#EEEEEE" align="center">
                                                            <td width="31" rowspan="2">No</td>
                                                            <td width="120" rowspan="2">SI Ref </td>
                                                            <td width="170" rowspan="2">Destination</td>
                                                            <td colspan="2">Quantity</td>
                                                            <td colspan="2">Delivered</td>
                                                            <td colspan="2">Shipping Period </td>
                                                        </tr>
                                                        <tr style="font-weight:bold" bgcolor="#EEEEEE" align="center">
                                                            <td width="70">Tons</td>
                                                            <td width="70">Bags</td>
                                                            <td width="70">Tons</td>
                                                            <td width="70">Bags</td>
                                                            <td width="100">From</td>
                                                            <td width="145">To</td>
                                                        </tr>
                                                    </table></td>
                                            </tr>
                                            <tr>
                                                <td><div style="overflow:scroll; height:150px;">
                                                        <table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                                            <%
                                                                    rcSI.beforeFirst();
                                                                    while (rcSI.next())
                                                                    {
                                                            %>
                                                            <tr id = <%=rcSI.getIntValue("id")%> class="style11" onClick="highlightOn(this,1)" onDblClick="cardViewClick_si(this)">
                                                                <td width="31" align="center" bgcolor="#EEEEEE"><%=rcSI.getIntValue("id")%></td>
                                                                <td width="120"><%=rcSI.getString("ref_number")%></td>
                                                                <td width="170"><%=task.getFieldValueForId("port_master","name",rcSI.getIntValue("port_discharge_id"))%></td>
                                                                <td width="70" align="right"><%=rcSI.getFloatValue("tons")%></td>
                                                                <td width="70" align="right"><%=rcSI.getFloatValue("no_of_bags")%></td>
                                                                <td width="70" align="right"><%=rcSI.getFloatValue("shipton")%></td>
                                                                <td width="70" align="right"><%=rcSI.getFloatValue("shipbag")%></td>
                                                                <td width="100" align="center"><%=rcSI.getDate("from_date")%></td>
                                                                <td width="128" align="center"><%=rcSI.getDate("to_date")%></td>
                                                            </tr>
                                                            <%
                                                                    }
                                                            %>
                                                        </table>
                                                    </div></td>
                                            </tr>
                                            <tr>
                                                <td><table width="100%"  border="0" cellpadding="1" cellspacing="1" class="style2">
                                                        <tr class="style2" bgcolor="#EEEEEE">
                                                            <td width="31" align="center"><img src="images/new.gif" width="15" height="15" title="Create New SI" onClick="doNewSi();"></td>
                                                            <td width="290" align="center"><strong>Total</strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td width="70" align="right"><strong><%%></strong></td>
                                                            <td align="center">&nbsp;</td>
                                                            <td width="15" align="center">&nbsp;</td>
                                                        </tr>
                                                    </table></td>
                                            </tr>					  
                                        </table></td>
                                </tr>
                            </table></td>
                    </tr>
                </table>
            </td>
        </tr>

    </table>
    <input type="hidden" name="uid"  id="uid"  value="<%=user.getuid()%>">
    <input type="hidden" name="task_id"  id="task_id"  value="0">
    <input type="hidden" name="view" id="view" value="<%=task.getRequest().getIntValue("view")%>">	
    <input name="new_rec" type="hidden" id="new_rec" value="0">
</form>

<%@include file="../footer.jsp"%>